package com.vitabuybuy.tv.dialog

import android.content.Context
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.library.common.toast.XToast
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.TaskPopupScreenBinding
import com.vitabuybuy.tv.dto.FansScopeDto
import com.vitabuybuy.tv.dto.FieldLabelDto
import razerdp.basepopup.BasePopupWindow

/**
 * 作者：SimGa
 * 日期：2021/4/10
 * 时间：17:29
 * 包名：com.vitabuybuy.tv.dialog
 */
class TaskScreenPopup(
    context: Context,
    type:String,
    bradList: MutableList<FieldLabelDto>?,
    fansScopeDto: FansScopeDto
) : BasePopupWindow(context) {
    private lateinit var mBinding: TaskPopupScreenBinding
    private lateinit var fieldLabelAdapter: FieldLabelAdapter_2

    init {
        if(type.isEmpty()){
            mBinding.rgType.check(R.id.btnLiveTask)
        }else{
            if(type == context.getString(R.string.live)){
                mBinding.rgType.check(R.id.btnLiveTask)
            }else{
                mBinding.rgType.check(R.id.btnPlanGrassTask)
            }
        }
        bradList?.run {
            fieldLabelAdapter.addData(this)
        }
        fansScopeDto.apply {
            if (limit > 0 ) {
                mBinding.etLowPrice.setText(limit.toString())
            }
            if (most > 0) {
                mBinding.etHighPrice.setText(most.toString())
            }
        }
    }


    override fun onCreateContentView(): View {
        return createPopupById(R.layout.task_popup_screen)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        mBinding = DataBindingUtil.bind<ViewDataBinding>(contentView) as TaskPopupScreenBinding
        fieldLabelAdapter = FieldLabelAdapter_2().apply {
            setOnItemClickListener { _, _, position ->
                for (bean in mData) {
                    bean.isChecked = false
                }
                mData[position].isChecked = true
                notifyDataSetChanged()
            }
        }
        mBinding.rcvBrand.apply {
            adapter = fieldLabelAdapter
            layoutManager = GridLayoutManager(context, 3)
        }
        setViewClickListener(View.OnClickListener {
            if (mBinding.rcvBrand.visibility == View.VISIBLE) {
                mBinding.rcvBrand.visibility = View.GONE
                mBinding.ivBrandSwitch.setImageResource(R.drawable.icon_common_arrow_down)
            } else {
                mBinding.rcvBrand.visibility = View.VISIBLE
                mBinding.ivBrandSwitch.setImageResource(R.drawable.icon_common_arrow_up)
            }
        }, mBinding.ivBrandSwitch)
        setViewClickListener(View.OnClickListener {
            if (mBinding.llPriceRange.visibility == View.VISIBLE) {
                mBinding.llPriceRange.visibility = View.GONE
                mBinding.ivPriceRangeSwitch.setImageResource(R.drawable.icon_common_arrow_down)
            } else {
                mBinding.llPriceRange.visibility = View.VISIBLE
                mBinding.ivPriceRangeSwitch.setImageResource(R.drawable.icon_common_arrow_up)
            }
        }, mBinding.ivPriceRangeSwitch)
        setViewClickListener(View.OnClickListener {
            if (mBinding.rgType.visibility == View.VISIBLE) {
                mBinding.rgType.visibility = View.GONE
                mBinding.ivDeliveryFromSwitch.setImageResource(R.drawable.icon_common_arrow_down)
            } else {
                mBinding.rgType.visibility = View.VISIBLE
                mBinding.ivDeliveryFromSwitch.setImageResource(R.drawable.icon_common_arrow_up)
            }
        }, mBinding.ivDeliveryFromSwitch)
        setViewClickListener(View.OnClickListener {
            val limitFans = mBinding.etLowPrice.text.toString()
            val highFans = mBinding.etHighPrice.text.toString()
            val fansDto = FansScopeDto().apply {
                if (limitFans.isNotEmpty() && highFans.isNotEmpty()) {
                    if (highFans > limitFans) {
                        limit = limitFans.toInt()
                        most = highFans.toInt()
                    }else{
                        XToast.normal(context.getString(R.string.fans_req_no_allow))
                    }
                }
            }
            val type = if (mBinding.btnLiveTask.isChecked)
                context.getString(R.string.live)
            else
                context.getString(R.string.plant_grass)
            onConfirm.invoke(
                fieldLabelAdapter.mData,
                fansDto, type
            )
            dismiss()
        }, mBinding.btnConfirm)
        setViewClickListener(View.OnClickListener {
            fieldLabelAdapter.apply {
                for (brand in mData) {
                    brand.isChecked = false
                }
                notifyDataSetChanged()
            }
            mBinding.etLowPrice.setText("")
            mBinding.etHighPrice.setText("")
            onReset.invoke()
            dismiss()
        }, mBinding.btnReset)
    }

    lateinit var onConfirm: (bradList: MutableList<FieldLabelDto>, fansScopeDto: FansScopeDto, type: String) -> Unit
    lateinit var onReset: () -> Unit
}